<template>
    <el-form ref="formRef" style="max-width: 600px" :model="formDate" :rules="rules" label-width="auto"
        class="demo-ruleForm" status-icon>
        <el-form-item label="商品的名称" prop="name">
            <el-input v-model="formDate.name" />
        </el-form-item>
        <el-form-item label="商品图片" prop="img">
            <Tuku v-model="formDate.img"></Tuku>
        </el-form-item>
        <el-form-item label="商品排序" prop="seq">
            <!-- <el-input v-model="formDate.seq" /> -->
            <el-input-number v-model="formDate.seq" :min="1" :max="10" />
        </el-form-item>
         <el-form-item label="商品价格" prop="price">
            <el-input-number v-model="formDate.price" :precision="2" :step="0.1" :max="10" />
        </el-form-item>
        <el-form-item label="父目录ID" prop="categoryId">
            <el-tree-select v-model="formDate.categoryId" :data="categorydata" check-strictly :render-after-expand="false"
                style="width: 180px" />
        </el-form-item>
         <el-form-item label="商品的介绍" prop="brief">
                    <!-- <el-input v-model="formDate.brief" /> -->
                    <Editors v-model="formDate.brief"></Editors>
                </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(formRef)">
                修改
            </el-button>
            <el-button @click="resetForm(formRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { CategoryApi ,ProductApi} from '@/api';
import Tuku from '@/components/Tuku.vue';
import { ElMessage } from 'element-plus'
import { useRouter, useRoute } from 'vue-router';
import Editors from '@/components/Editors.vue'
let router = useRouter()
let route=useRoute()
const formRef = ref()

const formDate = reactive({
    id:0,
    name: '',
    img: '',
    seq: 0,
    categoryId: 0, 
    price: 0,
    brief:''
})
const rules = reactive({
    name: [
        { required: true, message: '请输入您的姓名', trigger: 'blur' },
        { min: 1, max: 100, message: '最少长度是 1 to 5', trigger: 'blur' },
    ]
})
const categorydata = ref([
    {
        value: 0,
        label: '一级目录',

    },
])

const formSubmit = () => {
    ProductApi.update.call({
        id:formDate.id,
        name: formDate.name,
        img: formDate.img,
        seq: formDate.seq,
        categoryId: formDate.categoryId,
        price: formDate.price,
        brief:formDate.brief
    }).then((res: any) => {
        console.log(res);
        ElMessage.success("修改成功")
        //新增完成之后需要到哪里去
        router.push({ name: 'product-list' })
    })
}

onMounted(() => {
    console.log(route.query.id);
    
     formDate.id=Number(route.query.id)
    CallcategoryApi()
    CallEditgory()
})
//数据回显
const CallEditgory = () => { 

    ProductApi.select.call({ id: formDate.id }).then((res: any) => { 
        console.log(res);
        formDate.name = res[0].name
        formDate.img =res[0].img
        formDate.seq = res[0].seq
        formDate.categoryId = res[0].categoryId
        formDate.price = res[0].price
        formDate.brief=res[0].brief
    })

}


//查询 获取categoryapi
const CallcategoryApi = () => {
    CategoryApi.select.call().then((res: any) => {

        res.forEach((item: any) => {
            item.value = item.id
            item.label = item.name
        });
        //console.log(res);
        const cateGoryData2 = res.reduce((hezi: any, currentValue: any) => {
            //console.log(currentValue);

            currentValue.children = res.filter((item: any) => {
                return item.parentId === currentValue.id
            })
            if (currentValue.parentId === 0) {
                hezi.push(currentValue)
            }
            return hezi;
        }, [])
        console.log(cateGoryData2);
        categorydata.value = cateGoryData2
    })


}



const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            console.log('submit!')
            formSubmit()
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: any) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>